/*
 * 
 *方法的整理与重构
 *
 *
 */

//定义app参数名称，并加载其它模块
var authApp=angular.module('authapp', ["ngCookies",'ngFileUpload','ui.bootstrap']);

//此为定位页面所用
authApp.value('page','professionalSkillTest');

/*controller部分*/


//定义父controller，主要用于controller之间传递信息
authApp.controller("fcontroller",function($scope,$http){
	//
	//****与分页相关的****
	//
	
	//从接口中获取总的分页数量
	$scope.$on('$getPageCountFromApi',function(event,data){
		$scope.$broadcast('$getPageCount',data);
	});
	
	//捕获分页点击事件并传递currentPage信息到主controller
	 $scope.$on('$pageAction',function(event,data){
		 $scope.$broadcast('$pageChanged',data);
	 });
})


//主要操作controller
authApp.controller("skillTestListController",function($scope,$http,Upload,$timeout,$sce){
	//针对此页面需要的参数 should edit
	$scope.isAddM=true;
	$scope.isAddPwd=true;
	$scope.isclickBtn=false;
	
	//**********主体方法
	//设置分页初始参数（最好与$scope.currentPage和$scope.itemsPerPage一致！！！！//这边之所以$scope.pageNumber=0是因为后台接口第一页为0）
	$scope.ifFirstTimeQuery=true;
	$scope.pageNumber=0;
	$scope.pageSize=10;
	var queryMainApi=apis.queryAuthExpertTaskPage;//！！！should edit
	$scope.queryMain=function(){
		$http({
			method:'post',
			url:queryMainApi,
			data:{'pageNumber':$scope.pageNumber,'pageSize':$scope.pageSize},
		}).success(function(response){
			if(response.code=="000000"){
				$scope.queryMainApiCallBack(response);
				//将返回的pagecount和current传到上面的controller
				$scope.$emit('$getPageCountFromApi',response.data);
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("网络出现故障，请确认网络是否连接");
		})
	}
	//捕捉分页点击事件并做处理
	$scope.$on("$pageChanged",function(event,data){
		if(!$scope.ifFirstTimeQuery){//解决第一次会调用两次接口的问题
			$scope.pageNumber=data.currentPage-1;//这边之所以-1是因为后台接口第一页为0
			$scope.queryMain();
		}
		$scope.ifFirstTimeQuery=false;//改变是否是第一次调用接口的状态
	})
	$scope.queryMain();
	
	/*下面为自已编辑部分*/
	
	//调用主接口成功之后的处理方法
	$scope.queryMainApiCallBack=function(response){
		$scope.list=response.data.data;
        $scope.totalCount = response.data.recordCount;
        $scope.pageSize = response.data.pageSize;
		angular.forEach($scope.list,function(data,index,array){
			if(data.major=="0"){
				data.majorName="学前教育";
			}else if(data.major=="1"){
				data.majorName="小学教育";
			}else if(data.major=="2"){
				data.majorName="中学教育";
			}else{
				data.majorName="未选择";
			}
			
			if(data.taskType=="0"){
				data.taskTypeName="必测专业技能";
			}else if(data.taskType=="1"){
				data.taskTypeName="选测专业技能";
			}else if(data.taskType=="2"){
				data.taskTypeName="其他任务";
			}else{
				data.taskTypeName="未选择";
			}
		})
	}
	
	/*
	 * 
	 * 其它操作方法
	 * 
	 */
	
	//上传数据
	$scope.uploadFile="";
	$scope.uploadFiles= function(file, errFiles,index,taskId,groupId) {
		$scope.taskId01=taskId;
		$scope.groupId01=groupId;
        $scope.f = file;
        $scope.errFile = errFiles && errFiles[0];
        if (file) {
            file.upload = Upload.upload({
                url: apis.expertUploadTask,
                data: {file: file}
            });

            file.upload.then(function (response) {
                $timeout(function () {
                	if(response.data.code=="000000"){
                		$scope.fileName=file.name;
                		$scope.uploadFile=response.data.data;
                		$scope.saveExpertTask();
                	}
                	else
                		alert(response.data.errorMsg);
                });
            }, function (response) {
                if (response.status > 0)
                    $scope.errorMsg = response.status + ': ' + response.data;
            }, function (evt) {
                file.progress = Math.min(100, parseInt(100.0 * 
                evt.loaded / evt.total));
            });
        }
    }
	
	//按钮添加事件
	$scope.addM=function(taskId,groupId){
		$scope.isAddM=false;
		$scope.taskId=taskId;
		$scope.groupId=groupId;
		$scope.queryExpertExams();	
	}
	
	//按钮取消事件
	$scope.cancelAddM=function(){
		$scope.isAddM=true;
		$scope.addFZ="";
	}
	
	//查询专家对应专业技能抽取的题目
	$scope.queryExpertExams=function(){
		var rJson={'taskId':$scope.taskId,'groupId':$scope.groupId};
		$http({
			method:'get',
			url:apis.queryExpertExams,
			params:rJson
		}).success(function(response){
			if(response.code=="000000"){
				$scope.expertList=response.data;
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}
	
	//保存专家专业技能测试用表  -必测/选测    其他任务没有
	$scope.saveExpertTask=function(){
		var rJson={'taskId':$scope.taskId01,'groupId':$scope.groupId01,'fileUrl':$scope.uploadFile};
		$http({
			method:'post',
			url:apis.saveExpertTask,
			data:rJson
		}).success(function(response){
			if(response.code=="000000"){
				$scope.queryMain();
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}
	//添加机构信息
	/*$scope.addFirstZ=function(){
		if(window.confirm("是否确定添加？")){
			var rJson={'account':$scope.account,'schoolname':$scope.schoolName,'major':$scope.major};
			$http({
				method:'post',
				url:apis.insertAuthSchool,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.isAddM=true;
					$scope.queryMain();
					$scope.account="";
					$scope.schoolName="";
					$scope.major="";
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
				$scope.isAddM=true;
			})
		}
	}*/
	
	
	//根据id查询机构信息
	/*$scope.queryAuthSchoolInfoById=function(pid){		
		$scope.isAddM=false;
		$scope.isclickBtn=true;
		$scope.Id=pid;
		var random=Math.random();
		$http({
			method:'get',
			url:apis.queryAuthSchoolInfoById,
			params:{'pid':pid,'random':random}
		}).success(function(response){
			if(response.code=="000000"){
				$scope.account=response.data.account;
				$scope.schoolName=response.data.schoolName;
				$scope.major=response.data.major;				
			}else{
				alert(response.errorMsg);
			}
		}).error(function(){
			alert("服务器打瞌睡了，请稍等！");
		})
	}*/
	
	//修改机构信息
	/*$scope.updateAuthSchoolInfoAccount=function(){
		if(window.confirm("是否确定修改？")){
			var rJson={'pid':$scope.Id,'account':$scope.account,'schoolname':$scope.schoolName,'major':$scope.major};
			console.log(rJson);
			$http({
				method:'post',
				url:apis.updateAuthSchoolInfoAccount,
				data:rJson,
			}).success(function(response){
				if(response.code=="000000"){
					$scope.isAddM=true;
					$scope.queryMain();
					$scope.account="";
					$scope.schoolName="";
					$scope.major="";
				}else{
					alert(response.errorMsg);
				}
			}).error(function(){
				alert("服务器打瞌睡了，请稍等！");
				$scope.isAddM=true;
			})
		}
	}*/
	
	//下载模板
	$scope.expertDownLoad=function(pid){
		if(window.confirm("是否确定下载？")){
			location.href=apis.downloadExam+"?examId="+pid;
		}
	}
	
	//下载模板
	$scope.turnDownLoad=function(pid){
		if(window.confirm("是否确定下载？")){
			location.href=apis.downloadTask+"?taskId="+pid;
		}
	}
	
	//下载
	$scope.turnToDownLoad=function(pid){
		if(window.confirm("是否确定下载？")){
			location.href=apis.downloadFile+"?expertTaskId="+pid;
		}
	}
	
})

//分页controller
authApp.controller('pageDemo',function($scope){
	//定义分页的初始参数（不改动，不会影响功能）
	$scope.currentPage=1;
	$scope.itemsPerPage=10;
	$scope.maxSize = 8;
	//需要接口返回的一些参数，如pagecount
	$scope.$on('$getPageCount',function(event,data){
        $scope.totalItems = parseInt(data.recordCount);
        $scope.bigTotalItems =parseInt(data.recordCount);      
	});
	//分页点击的触发事件
	 $scope.$watch('currentPage', function() {
		 var pagedata={currentPage:$scope.currentPage};
		 $scope.$emit('$pageAction',pagedata);
	  });
})